//
//  SearchPackageViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/19.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "SearchPackageViewController.h"

@interface SearchPackageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;

@property (nonatomic, strong) NSMutableArray <CommonPackageModel *>*dataArray;

@property (nonatomic, strong) UITableView *tableview;

@end

@implementation SearchPackageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
    [self refreshParam];
    [self request_searchStorePackage];
}

- (void)refreshParam {
    
    self.pageNum = 1;
    self.pageSize = 10;
}

- (void)initView {
    
    self.title = @"选择套餐";
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    [self initViewAction];
}


- (void)initViewAction {
    

}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
//    return 10;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return  [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ImSearchPackageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ImSearchPackageCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.dataArray[indexPath.row]];

    WEAKSELF
    cell.ImSearchPackageCellBlock = ^{
        if (weakSelf.SearchPackageVCBlock) {
            weakSelf.SearchPackageVCBlock(weakSelf.dataArray[indexPath.row]);
        }
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    
    return cell;
}

- (NSMutableArray<CommonPackageModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);

        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"ImSearchPackageCell" bundle:nil] forCellReuseIdentifier:@"ImSearchPackageCellID"];
        
        WEAKSELF
        _tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            [weakSelf refreshParam];
            [weakSelf request_searchStorePackage];
        }];

        _tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            weakSelf.pageNum ++;
            [weakSelf request_searchStorePackage];
        }];
        _tableview.mj_footer.ignoredScrollViewContentInsetBottom = Is_IPhoneX ? Bottom_SafeHeight : CGFLOAT_MIN;


    }
    return _tableview;
}

- (void)request_searchStorePackage {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    
    [dic setValue:@(self.pageNum) forKey:@"pageNum"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];

    NSDictionary *t = @{}.mutableCopy;
    [t setValue:self.storeId forKey:@"sellerId"];
    [t setValue:[TTUserModel sharedObject].cityId forKey:@"sellerCityId"];

    [dic setValue:t forKey:@"t"];

    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_StorePackage];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];

        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                
                if ([resultDic isKindOfClass:[NSDictionary class]]) {

                
                    NSArray *list = [resultDic valueForKey:@"list"];

                    if (weakSelf.pageNum == 1) {
                        weakSelf.dataArray = @[].mutableCopy;
                    }
                    
                    if ([list isKindOfClass:[NSArray class]]) {
                        for (int i = 0; i < list.count; i++) {
                            NSDictionary *listDic = list[i];
                            CommonPackageModel *model = [CommonPackageModel modelWithJSON:listDic];
                            NSString *url = [TT_OSSManager reSourceUrl:model.fangUrl type:BA_resizeType_pro];
                            model.fangUrl = url;
                            
                            [weakSelf.dataArray addObject:model];
                        }
                    }
                    
                    if (weakSelf.dataArray.count == 0) {
                        [[TTNoResultManager shareManager] showNoResultWithStateInView:weakSelf.tableview errorCode:kTTViewState_NoSearchResult btnTitle:@"" didPressButton:^{
                        }];
                    }
                    [weakSelf.tableview reloadData];
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
    
}

@end
